package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.example.common.R;
import org.example.entity.Business;
import org.example.entity.Employee;
import org.example.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * TODO 外卖员
 *
 * @author shaoxuan.
 * @date 2023/12/23 11:38
 */
@Slf4j
@RestController
@RequestMapping("/employee")
@Api(tags = "外卖员管理")

public class EmployeeController {
    @Autowired
    private EmployeeService service;


    /**
     * 外卖员注册
     *
     * @param employee
     * @return
     */
    @PostMapping("/register")
    @ApiOperation(value = "外卖员注册")

    public R register(@RequestBody Employee employee) {
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        Employee one = service.getOne(new LambdaQueryWrapper<Employee>().
                eq(Employee::getUsername, employee.getUsername()));
        if (one == null) {
            employee.setPassword(password);
            service.save(employee);
            return R.success("注册成功");
        } else {
            return R.error("用户名已存在，注册失败");
        }


    }

    /**
     * 外卖员登录
     *
     * @param request
     * @param employee
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "外卖员登录")

    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {
        //1、将页面提交的密码password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2、根据页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = service.getOne(queryWrapper);

        //3、如果没有查询到则返回登录失败结果
        if (emp == null) {
            return R.error("登录失败");
        }

        //4、密码比对，如果不一致则返回登录失败结果
        if (!emp.getPassword().equals(password)) {
            return R.error("登录失败");
        }

        //5、查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0) {
            return R.error("账号已禁用");
        }

        //6、登录成功，将员工id存入Session并返回登录成功结果
        request.getSession().setAttribute("employee", emp.getId());
        return R.success(emp);
    }

    /**
     * 退出
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")

    public R<String> logout(HttpServletRequest request) {
        //清理Session中保存的当前登录员工的id
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 根据id获取外卖员信息
     *
     * @param employeeId
     * @return
     */
    @GetMapping("/getInfo/{employeeId}")
    @ApiOperation(value = "根据id获取外卖员信息")

    public R<Employee> getInfoBy(@PathVariable("employeeId") String employeeId) {
        Employee one = service.getOne(new LambdaQueryWrapper<Employee>()
                .eq(Employee::getId, employeeId));
        return R.success(one);
    }

    /**
     * 修改外卖员信息
     *
     * @param employee
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改外卖员信息")

    public R<String> update(@RequestBody Employee employee) {
        if (employee.getId() != null) {
            service.updateById(employee);
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }

    @GetMapping("/getAll")
    @ApiOperation(value = "获取所有外卖员信息")
    public R<List<Employee>> getAll(){
        List<Employee> list = service.list();
        return R.success(list);
    }
}
